const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBindPhone: false,
    phone : '',
    dialogThemeShow: false,
    skin: '',
    skinList: [{
      name: '主题-深色',
      skin: 'skin-normal',
      backgroundColor: '#005CAC'
    }, {
      name: '主题-浅色',
      skin: 'skin-light',
      backgroundColor: '#FAFAFA'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let userInfo = wx.getStorageSync('userinfo');
    if (userInfo) {
      that.setData({
        "isBindPhone" : true,
        "phone" : userInfo.miniAccountInfo.phone
      });
    }  else {
      utils.wxlogin().then(function (wxloginRes) {
        utils.promiseRequest('/wx/user/getMiniAccountByCode', 'GET', {"code" : wxloginRes.code}).then(function(res){
          that.setData({
            "isBindPhone" : true,
            "phone" : res.data.phone
          });
        }).catch(function(e){
          console.log(e);
        });
      }).catch(function(e){
        console.log(e);
      });
    }

    app.setNavBar()
    const { skin = 'skin-normal' } = app.globalData
    this.setData({
      skin
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  phoneindex() {
    let that = this;
    wx.navigateTo({
      url: "/pages/phone/index?phone=" + that.data.phone
    });
  },

  showTheme() {
    this.setData({
      dialogThemeShow: true
    })
  },
  hideTheme() {
    this.setData({
      dialogThemeShow: false
    })
  },
  onThemeChange(e) {
    const { skin } = this.data
    const newSkin = e.currentTarget.dataset.skin
    if (skin === newSkin) {
      return false
    } else {
      this.setData({ skin: newSkin })
      app.globalData.skin = newSkin
      app.saveSkin()
      this.hideTheme()
      app.setNavBar()
    }
  }
})